package com.shigen.file.util;

import java.text.DecimalFormat;

/**
 * 格式化时间工具类
 *
 * @author shigenfu
 * @date 2022/6/13 1:15 下午
 */
public class TimeUtil {

  /**
   * 格式化秒
   */
  public static String formatSec(long seconds) {
    DecimalFormat formater = new DecimalFormat("####.00");
    if (seconds < 60) {
      return seconds + "秒";
    } else if (seconds < 60 * 60) {
      float min = seconds / 60f;
      return formater.format(min) + "分钟";
    } else if (seconds < 60 * 60 * 24) {
      float hour = seconds / 3600f;
      return formater.format(hour) + "小时";
    } else if (seconds < 60 * 60 * 24 * 30) {
      float day = seconds / 3600f / 24f;
      return formater.format(day) + "天";
    } else if (seconds < 60 * 60 * 24 * 30 * 12) {
      float month = seconds / 3600f / 24f / 30f;
      return formater.format(month) + "月";
    } else if (seconds >= 60 * 60 * 24 * 30 * 12) {
      float year = seconds / 3600f / 24f / 30f / 12f;
      return formater.format(year) + "年";
    } else {
      return "size: error";
    }

  }
}
